package hm.edu.cs;


import java.util.ArrayList;
import java.util.List; 
import android.app.ListActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.SearchManager;
import android.app.TabActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class SearchBookActivity extends ListActivity implements OnClickListener, Runnable{


	public static final String name = "SearchBookActivity";
	private ProgressDialog dialog = null;
	protected EditText searchString;
	protected ImageButton searchButton;
	HTTPSConnection connection;
	BookList bookList;
	//String query;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.e(null, "onCreate Search Activity");
		//this.getListView().setDividerHeight(2);
		setContentView(R.layout.search_main);
		//lvlList=(ListView)findViewById(R.id.ListView01);
		
		connection = new HTTPSConnection();
		
		searchButton = (ImageButton) findViewById(R.id.searchButton);
		searchString = (EditText) findViewById(R.id.searchText);
		searchButton.setOnClickListener(this);
		
		Bundle extras = getIntent().getExtras();
		
		if(extras !=  null && extras.containsKey("booklist"))
		{
			bookList = (BookList) extras.getParcelable("booklist");
		    setListAdapter(new BookListAdapter(bookList, this));
		}
		
	}	
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);

		//Intent i = new Intent(getParent(),hm.edu.cs.DetailBookActivity.class);
		TabGroupActivity parentActivity = (TabGroupActivity) getParent();
		Bundle b = new Bundle();
		Log.e(null, "Stecke Buch in bundle: "+ bookList.getBook(position));
		b.putParcelable("book", bookList.getBook(position));
		b.putParcelable("booklist", bookList);
		
		Intent intent = new Intent(SearchBookActivity.this, DetailBookActivity.class);
		intent.putExtras(b);
		intent.putExtra("sourceActivity", SearchBookActivity.name);
		parentActivity.startChildActivity("SearchBookActivity", intent);

	}

	 @Override
	    protected Dialog onCreateDialog(int id) {
	        switch (id) {
	            case 1: {
	                dialog = new ProgressDialog(this);
	                dialog.setMessage("Please wait...Dowloading data...");
	                dialog.setIndeterminate(true);
	                dialog.setCancelable(true);
	                return dialog;
	            }
	        }
	        return null;
	    }
	
	@Override
	public void onClick(View v) {
		//showDialog(1);
		new Thread(SearchBookActivity.this).start();
	}
	
	 private Handler handler = new Handler(){
			
			@Override
			public void handleMessage(Message msg) {
				if (dialog != null)
					dialog.dismiss();	
				
			    setListAdapter(new BookListAdapter(bookList, SearchBookActivity.this));
			}
		};

	@Override
	public void run() {
		Looper.prepare();
		Book books[] = connection.createData(this, searchString.getText().toString());
		if(books != null)
		{
			bookList = new BookList(books);	
			handler.sendEmptyMessage(0);
		}
		else// sonst fehler
		{
			// createAlertDialog("No Results found. Try Again...");
		}
		
		Looper.loop();
	}
	
	private void createAlertDialog(String message){
		AlertDialog.Builder alertbox = new AlertDialog.Builder(SearchBookActivity.this);
        
        // set the message to display
        alertbox.setMessage(message);

        // add a neutral button to the alert box and assign a click listener
        alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {

            // click listener on the alert box
            public void onClick(DialogInterface arg0, int arg1) {
                // the button was clicked
            	if (dialog != null)
        	   		dialog.dismiss();
            }
        });

        // show it
        alertbox.show();
	}

}
